#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
专门检查C_Header_Statistics.prefab文件的脚本
"""

import re
from pathlib import Path

# 节点名称映射表
NODE_NAME_MAPPING = {
    # 顶部统计相关
    "收款总数背景色": "Revenue_Total_Background",
    "收款总数背景色-值": "Revenue_Total_Value",
    "吃码总数背景色": "Eat_Code_Total_Background", 
    "吃码总数背景色-值": "Eat_Code_Total_Value",
    "上报总数背景色": "Report_Total_Background",
    "上报总数背景色-值": "Report_Total_Value",
    "文本": "Text",
}

def chinese_to_unity_unicode(chinese_text):
    """将中文字符串转换为Unity格式的Unicode转义序列（大写）"""
    unicode_escaped = chinese_text.encode('unicode_escape').decode('ascii')
    # 将小写的unicode转义序列转换为大写
    return re.sub(r'\\u([0-9a-f]{4})', lambda m: f'\\u{m.group(1).upper()}', unicode_escaped)

def check_file():
    """检查C_Header_Statistics.prefab文件"""
    file_path = Path("Assets/Prefabs/C_Header_Statistics.prefab")
    
    if not file_path.exists():
        print(f"文件不存在: {file_path}")
        return
    
    print(f"检查文件: {file_path}")
    
    # 读取文件内容
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    print("\n=== 查找所有m_Name字段 ===")
    name_patterns = re.findall(r'm_Name: "([^"]*)"', content)
    for i, name in enumerate(name_patterns[:20]):  # 只显示前20个
        print(f"{i+1:2d}. {name}")
    
    print(f"\n总共找到 {len(name_patterns)} 个m_Name字段")
    
    print("\n=== 查找Unicode编码的中文 ===")
    unicode_patterns = re.findall(r'm_Name: "([^"]*\\u[0-9a-fA-F]{4}[^"]*)"', content)
    for i, name in enumerate(unicode_patterns):
        print(f"{i+1:2d}. {name}")
    
    print(f"\n总共找到 {len(unicode_patterns)} 个Unicode编码的中文节点")
    
    print("\n=== 检查映射表中的中文是否存在 ===")
    found_mappings = {}
    for chinese_name, english_name in NODE_NAME_MAPPING.items():
        unity_unicode = chinese_to_unity_unicode(chinese_name)
        pattern = rf'm_Name: "{re.escape(unity_unicode)}"'
        matches = re.findall(pattern, content)
        if matches:
            found_mappings[chinese_name] = len(matches)
            print(f"✓ 找到: {chinese_name} -> {unity_unicode} ({len(matches)}次)")
        else:
            print(f"✗ 未找到: {chinese_name} -> {unity_unicode}")
    
    if found_mappings:
        print(f"\n需要替换的节点: {len(found_mappings)} 种")
        return True
    else:
        print("\n没有找到需要替换的中文节点")
        return False

if __name__ == "__main__":
    check_file() 